﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using JuCheap.Core.Infrastructure.Extentions;
using JuCheap.Core.Interfaces;
using JuCheap.Core.Models.Filters;
using JuCheap.Core.Models.ViewModels;
using JuCheap.Core.Web.Filters;
using JuCheap.Core.Web.Models;
using Microsoft.AspNetCore.Mvc;

namespace JuCheap.Core.Web.Controllers
{
    public class HardWareController : Controller
    {
        private readonly IRepository _iRepository;
        public HardWareController(IRepository iRepository)
        {
            _iRepository = iRepository;
        }
        public IActionResult Index()
        {
            return View();
        }

        public async Task<IActionResult> Delete([FromBody]List<string> ids)
        {
            var result = new JsonResultModel<bool>();
            if (ids.AnyOne())
            {
                result.flag = true;
                foreach (string str in ids)
                {
                    await _iRepository.DeleteHardWareDevice(Convert.ToInt32(str));
                }
            }
            return Json(result);
        }
        public string GetStatus(int status)
        {
            if (status == 1)
                return "启用";
            return "停用";
        }
        /// <summary>
        /// 搜索页面
        /// </summary>
        /// <param name="filters">查询参数</param>
        /// <returns></returns>
        [IgnoreRightFilter]
        public async Task<IActionResult> GetListWithPager(HardWareDeviceFilter filters)
        {
            var result = await _iRepository.GetHardWareDevices(filters);
            
            foreach(var r in result.rows)
            {
                r.State = GetStatus(r.Status);
            }
            return Json(result);
        }


        public IActionResult Add()
        {
            return View(new HardWareDeviceViewModel());
        }


        /// <summary>
        /// 获取树
        /// </summary>
        /// <param name="q">查询关键字</param>
        /// <returns></returns>
        [HttpGet, IgnoreRightFilter]
        public async Task<ActionResult> GetHardWares(string q)
        {
            var filter = new HardWareDeviceFilter
            {
                page = 1,
                rows = 1000,
                keywords = null
            };
            var result = await _iRepository.GetHardWareDevices(filter);
            var depts = new
            {
                results = result.rows.Select(x => new { id = x.Id, text = x.Name })
            };
            return Json(depts);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Add(HardWareDeviceViewModel organization)
        {
            if (ModelState.IsValid)
            {
                var dto = await _iRepository.GetHardWareDevices(new HardWareDeviceFilter() { DeviceSn = organization.DeviceSn, });
                if (dto == null || dto.rows.Count == 0)
                {
                    var result = await _iRepository.AddHardWareDevice(organization);
                //if (result.IsNotBlank())
                    return RedirectToAction("Index");
                }
                else
                {
                    ModelState.AddModelError(nameof(organization.Name), "此设备已经存在，请勿重复添加！");
                }
            }
            return View(organization);
        }


        public async Task<IActionResult> Edit(int id)
        {
            var dto = await _iRepository.GetHardWareDevices(new HardWareDeviceFilter() { id = id, });

            if (dto == null || dto.rows.Count == 0)
            {
                return RedirectToAction("Add");
            }
            return View(dto.rows[0]);
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Edit(HardWareDeviceViewModel model)
        {
            if (ModelState.IsValid)
            {
                var result = await _iRepository.UpdateHardWareDevice(model);
                return RedirectToAction("Index");
            }
            return View(model);
        }
    }
}